package oa.hleast.xswl.controller.app.clockin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.annotation.UserAuth;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.clockin.UmsClockInForm;
import oa.hleast.xswl.pojo.vo.clockin.UmsClockInVo;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.clockin.IClockInService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "【移动端】用户打卡 ")
@RestController("ClockInController")
@RequestMapping("/api.app/v1/clockIn")
@Slf4j
@AllArgsConstructor
public class ClockInController {

    private static final String RESEARCH = "research";

    private final IClockInService clockInService;
    private final IUmsUserService iUmsUserService;

    @ApiOperation(value = "研学地图用户打卡", httpMethod = "POST")
    @PostMapping("/clockIn")
    public Result<Boolean> clockIn(@RequestBody UmsClockInForm form) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto user = iUmsUserService.getUserDtoById(userId);
        if (user == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        form.setUserId(userId);
        form.setSourceType(RESEARCH);
        boolean result = clockInService.add(form);

        return Result.success(result);
    }

    @UserAuth
    @ApiOperation(value = "研学地图用户打卡记录", httpMethod = "GET")
    @GetMapping("/findRedTourismClockInLog")
    Result<List<UmsClockInVo>> findRedTourismClockInLog() {
        Long userId = RequestUtils.getUserId();
        UmsUserDto user = iUmsUserService.getUserDtoById(userId);
        if (user == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        List<UmsClockInVo> voList = clockInService.findUserClockInLog(userId, RESEARCH);
        return Result.success(voList);
    }

    @ApiOperation(value = "用户打卡记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sourceType", value = "打卡来源", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/findUserClockInLog")
    Result<List<UmsClockInVo>> findUserClockInLog(@RequestParam @NotNull Long userId, @RequestParam @NotBlank String sourceType) {
        List<UmsClockInVo> voList = clockInService.findUserClockInLog(userId, sourceType);
        return Result.success(voList);
    }

    @GetMapping("/isClockIn")
    Result<Boolean> isClockIn(@RequestParam @NotNull Long placeInfoId, @RequestParam @NotNull Long userId, @RequestParam @NotBlank String sourceType) {
        boolean result = clockInService.isClockIn(placeInfoId, userId, sourceType);
        return Result.success(result);
    }
}
